/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditEmployeePanel.java
 *
 * Created on Jan 30, 2012, 8:30:04 PM
 */
package ui.panels;

import entities.Department;
import entities.User;
import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import manager.GenericDriver;
import manager.ManagerDriver;
import model.ViewDepartmentComboBoxModel;
import ui.windows.RemedySchedulerFrame;

/**
 *
 * @author viev
 */
public class EditEmployeePanel extends javax.swing.JPanel {

    /** Creates new form EditEmployeePanel */
    public EditEmployeePanel(RemedySchedulerFrame mainFrame, EmployeeManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        driver = (ManagerDriver) mainFrame.getDriver();
        genDriver = mainFrame.getGenericDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        roleModel = new DefaultComboBoxModel<String>(roles);
        roleModel.setSelectedItem("Employee");
        levelModel = new DefaultComboBoxModel<String>(levels[2]);
        cbRoles.setModel(roleModel);
        cbLevels.setModel(levelModel);

        depModel = new ViewDepartmentComboBoxModel(genDriver.showDepartment());
        depModel.setViewEmployee();
        cbDeps.setModel(depModel);

        checkDate = false;
        lbCheckDate.setText("");
        checkChange = false;
    }

    private void updateEmployee() {
        checkDate();
        if (checkDate) {
            Department dep = (Department) cbDeps.getSelectedItem();
            if (dep != null) {
                int id = dep.getDepartmentID();
                int role = cbRoles.getSelectedIndex() + 2;
                int level = cbLevels.getSelectedIndex() + 1;
                Date birthDate = dcBirthDate.getDate();              
                User updateRecord = new User();
                updateRecord.setUserID(selectedEmployee.getUserID());
                updateRecord.setName(txtName.getText());
                updateRecord.setLoginID(selectedEmployee.getLoginID());
                updateRecord.setLoginPass(selectedEmployee.getLoginPass());
                updateRecord.setStatus(selectedEmployee.getStatus());
                updateRecord.setTitle(selectedEmployee.getTitle());
                updateRecord.setDepartmentID(id);
                updateRecord.setDepartmentName(dep.getName());
                updateRecord.setRole(role);
                updateRecord.setLevel(level);
                updateRecord.setMale(rbMale.isSelected());
                updateRecord.setBirthDate(birthDate);
                updateRecord.setPhone(txtPhone.getText());
                updateRecord.setEmailAddress(txtEmail.getText());
                updateRecord.setAddress(txaAddress.getText());
                checkChange(updateRecord);
                if (checkChange) {
                    boolean returnVal = parentPanel.updateEmployee(updateRecord);
                    if (returnVal) {
                        close();
                        JOptionPane.showMessageDialog(mainFrame, "Updated", "message", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(mainFrame, "Cannot update !", "Error", JOptionPane.WARNING_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(mainFrame, "You not assign this employee !");
                }
            } else {
                JOptionPane.showMessageDialog(mainFrame, "You migh choise department !");
            }
        }
    }

    private void checkChange(User newE) {
        if (!newE.getName().equals(selectedEmployee.getName())) {
            checkChange = true;
        } else {
            if (newE.getDepartmentID() != selectedEmployee.getDepartmentID()) {
                checkChange = true;
            } else {
                if (newE.getRole() != selectedEmployee.getRole()) {
                    checkChange = true;
                } else {
                    if (newE.getLevel() != selectedEmployee.getLevel()) {
                        checkChange = true;
                    } else {
                        if ((!newE.isMale() && selectedEmployee.isMale()) || (newE.isMale() && !selectedEmployee.isMale())) {  
                            checkChange = true;
                        } else {
                            if (!newE.getBirthDate().equals(selectedEmployee.getBirthDate())) {  
                                checkChange = true;
                            } else {
                                if (!newE.getPhone().equals(selectedEmployee.getPhone())) {
                                    checkChange = true;
                                } else {
                                    if (!newE.getEmailAddress().equals(selectedEmployee.getEmailAddress())) {
                                        checkChange = true;
                                    } else {
                                        if (!newE.getAddress().equals(selectedEmployee.getAddress())) {
                                            checkChange = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public void setSelectedEmployee(User selectedEmployee) {
        this.selectedEmployee = selectedEmployee;
        if (this.selectedEmployee != null) {
            lbID.setText(selectedEmployee.getLoginID());
            txtName.setText(selectedEmployee.getName());
            cbDeps.setSelectedIndex(selectedEmployee.getDepartmentID() - 2);
            int choise = selectedEmployee.getRole() - 2;
            cbRoles.setSelectedIndex(choise);
            setRole();
            cbLevels.setSelectedIndex(selectedEmployee.getLevel() - 1);
            dcBirthDate.setDate(selectedEmployee.getBirthDate());
            rbMale.setSelected(selectedEmployee.isMale());
            txtPhone.setText(selectedEmployee.getPhone());
            txtEmail.setText(selectedEmployee.getEmailAddress());
            txaAddress.setText(selectedEmployee.getAddress());
        } else {
            clear();
        }
    }

    private void checkDate() {
        Date birtDate = dcBirthDate.getDate();
        if (birtDate != null) {
            if (birtDate.before(Calendar.getInstance().getTime())) {
                lbCheckDate.setText("valid");
                lbCheckDate.setForeground(new Color(102, 102, 102));
                checkDate = true;
            } else {
                lbCheckDate.setText("invalid !");
                lbCheckDate.setForeground(Color.red);
                checkDate = false;
            }
        } else {
            lbCheckDate.setText("enter birthdate...");
            lbCheckDate.setForeground(Color.blue);
            checkDate = false;
        }
    }

    private void setRole() {
        int choise = cbRoles.getSelectedIndex();
        if (choise != -1) {
            levelModel = new DefaultComboBoxModel<String>(levels[choise]);
            cbLevels.setModel(levelModel);
            cbLevels.setSelectedIndex(0);
        }
    }

    private void clear() {
        lbID.setText("");
        txtName.setText("");
        cbDeps.setSelectedItem(null);
        cbRoles.setSelectedIndex(2);
        levelModel = new DefaultComboBoxModel<String>(levels[2]);
        cbLevels.setSelectedIndex(0);
        rbMale.setSelected(true);
        dcBirthDate.setDate(null);
        txtPhone.setText("");
        txtEmail.setText("");
        txaAddress.setText("");
        lbCheckDate.setText("");
        checkDate = false;
        checkChange = false;
    }

    private void close() {
        clear();
        parentPanel.closeTarget();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaAddress = new javax.swing.JTextArea();
        lbID = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        cbDeps = new javax.swing.JComboBox();
        cbRoles = new javax.swing.JComboBox();
        cbLevels = new javax.swing.JComboBox();
        rbMale = new javax.swing.JRadioButton();
        rbFemale = new javax.swing.JRadioButton();
        dcBirthDate = new com.toedter.calendar.JDateChooser();
        lbCheckDate = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        bUpdate = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Employee's Detals", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel2.setText("ID:");

        jLabel3.setText("Name:");

        jLabel4.setText("Department:");

        jLabel5.setText("Role:");

        jLabel6.setText("Level:");

        jLabel7.setText("Gender:");

        jLabel8.setText("BirthDate:");

        jLabel9.setText("Phone:");

        jLabel10.setText("Email:");

        jLabel11.setText("Address:");

        txaAddress.setColumns(20);
        txaAddress.setRows(5);
        jScrollPane1.setViewportView(txaAddress);

        lbID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbID.setText("value");

        cbDeps.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbRoles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbRoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbRolesActionPerformed(evt);
            }
        });

        cbLevels.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        rbMale.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rbMale);
        rbMale.setSelected(true);
        rbMale.setText("male");

        rbFemale.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rbFemale);
        rbFemale.setText("female");

        lbCheckDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckDate.setText("value");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel8))
                        .addGap(58, 58, 58)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbID)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(rbMale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbFemale))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(cbLevels, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cbRoles, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cbDeps, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(dcBirthDate, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbCheckDate))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(83, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbDeps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cbRoles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbLevels, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(rbMale)
                            .addComponent(rbFemale))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(dcBirthDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lbCheckDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/close_20.png"))); // NOI18N
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bUpdate)
                .addContainerGap(51, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateActionPerformed
    updateEmployee();
}//GEN-LAST:event_bUpdateActionPerformed

private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
    close();
}//GEN-LAST:event_jLabel1MouseClicked

private void cbRolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbRolesActionPerformed
    setRole();
}//GEN-LAST:event_cbRolesActionPerformed
    private RemedySchedulerFrame mainFrame;
    private EmployeeManagementPanel parentPanel;
    private ManagerDriver driver;
    private GenericDriver genDriver;
    private DefaultComboBoxModel<String> roleModel, levelModel;
    private ViewDepartmentComboBoxModel depModel;
    private String[] roles = {"Administractor", "Department Head", "Employee"};
    private String[][] levels = {{"Head", "Employee"},
        {"Head", "Immediate Head", "Next Immediate Head", "Junior Head "},
        {"Doctor", "Staff"}};
    private User selectedEmployee;
    private boolean checkDate;
    private boolean checkChange;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbDeps;
    private javax.swing.JComboBox cbLevels;
    private javax.swing.JComboBox cbRoles;
    private com.toedter.calendar.JDateChooser dcBirthDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbCheckDate;
    private javax.swing.JLabel lbID;
    private javax.swing.JRadioButton rbFemale;
    private javax.swing.JRadioButton rbMale;
    private javax.swing.JTextArea txaAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables
}
